<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "org-info",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";
import { Plugins } from "/#/crud";
import { reactive } from "vue";

const { service } = useCool();
const { t } = useI18n();

// 选项
const options = reactive({
	type: [
		{ label: t("总机构"), value: 0, type: "danger" },
		{ label: t("分支机构"), value: 1, type: "success" },
	],
	status: [
		{ label: t("停用"), value: 0, type: "danger" },
		{ label: t("启用"), value: 1, type: "success" },
	],
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t("名称"),
			prop: "name",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("简称"),
			prop: "shortName",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{ label: t("Logo"), prop: "logo", component: { name: "cl-upload" } },
		{
			label: t("机构类型"),
			prop: "type",
			component: { name: "el-radio-group", options: options.type },
			value: 0,
			required: true,
		},
		{
			label: t("联系人"),
			prop: "contact",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("联系电话"),
			prop: "phone",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("地址"),
			prop: "address",
			component: {
				name: "el-input",
				props: { type: "textarea", rows: 4 },
			},
		},
		{
			label: t("状态"),
			prop: "status",
			component: { name: "el-radio-group", options: options.status },
			value: 1,
			required: true,
		},
		{
			label: t("排序号"),
			prop: "orderNo",
			hook: "number",
			component: { name: "el-input-number", props: { min: 0 } },
			span: 12,
			required: true,
		},
		{
			label: t("选择上级机构"),
			prop: "parentId",
			component: {
				name: "cl-select",
				props: {
					tree: true,
					current: true,
					labelKey: "name",
					valueKey: "id",
					checkStrictly: true,
				},
			},
			span: 12,
		},
	],
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: t("名称"), prop: "name", minWidth: 140, align: "left" },
		{ label: t("名称"), prop: "parentName", minWidth: 140 },
		{ label: t("简称"), prop: "shortName", minWidth: 140 },
		{
			label: t("Logo"),
			prop: "logo",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } },
		},
		{
			label: t("机构类型"),
			prop: "type",
			minWidth: 120,
			dict: options.type,
		},
		{ label: t("联系人"), prop: "contact", minWidth: 140 },
		{ label: t("联系电话"), prop: "phone", minWidth: 140 },
		{
			label: t("地址"),
			prop: "address",
			showOverflowTooltip: true,
			minWidth: 200,
		},
		{
			label: t("状态"),
			prop: "status",
			minWidth: 120,
			dict: options.status,
		},
		{
			label: t("排序号"),
			prop: "orderNo",
			minWidth: 140,
			sortable: "custom",
		},
		{
			label: t("创建时间"),
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" },
		},
		{
			label: t("更新时间"),
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" },
		},
		{
			type: "op",
			width: 250,
			buttons: [
				{
					label: t("新增"),
					hidden: !service.org.info._permission?.add,
					type: "success",
					onClick: ({ scope }) => {
						Crud.value?.rowAppend({ parentId: scope.row.id });
					},
				},
				"edit",
				"delete",
			],
		},
	],
	plugins: [Plugins.Table.toTree()],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.org.info,
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
